//
//  ViewController.m
//  15-NetEaseHomeWork
//
//  Created by tusm on 16/8/3.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ViewController.h"
#import "NetEaseCollectionViewCell.h"
#import "NetEaseTitleLayout.h"
#import "TList.h"
#define kCollectionViewReuse @"cellReuse"
#define kArc4RandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:0.5]

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource, NetEaseTitltLayoutDelegate>
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NetEaseTitleLayout *layout;
@property (nonatomic,strong)UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _height = 44;
   self.titleArray = [NSMutableArray array];
    [self handleAllUrl];
    [self createCollectionView];
}
- (void)handleAllUrl{
    
    NSURL *allUrl = [NSURL URLWithString:@"http://c.m.163.com/nc/topicset/android/subscribe/manage/listspecial.html"];
    NSMutableURLRequest *allURLRequest = [NSMutableURLRequest requestWithURL:allUrl];
    allURLRequest.HTTPMethod = @"GET";
    NSURLSession *urlsession = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [urlsession dataTaskWithRequest:allURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSError *error = nil;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            NSArray *array = dic[@"tList"];
            for (NSDictionary *dictionary in array) {
//                NSLog(@"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%@",dictionary);
//                TList *tlist = [[TList alloc] initWithDictionary:dictionary];
                NSString *name = dictionary[@"tname"];
                [_titleArray addObject:name];
            }
            [_collectionView reloadData];
        });
    }];
    
    [dataTask resume];
}

- (void)createCollectionView {
    self.layout = [[NetEaseTitleLayout alloc] init];
    _layout.titleFont = [UIFont systemFontOfSize:17];
    _layout.delegate =self;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.bounds), _height) collectionViewLayout:_layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"NetEaseCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionViewReuse];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NetEaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.backgroundColor = kArc4RandomColor;
    cell.titleLabel.text = _titleArray[indexPath.row];
    cell.titleLabel.font = _layout.titleFont;
    return cell;
}
#pragma mark - layout协议方法
- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
  widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = _titleArray[indexPath.row];
    CGRect newRect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, _height)
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName : _layout.titleFont}
                                       context:nil];
    return newRect.size.width;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
